<template>
	<view class="page">
		<view class="top">{{className}}</view>
		<view class="homeworkItem" @click="jumpToHomework(index)" v-for="(item, index) in studentList" :key="index">
			<view class="homeworkItem_title">
				<text>{{item.studentName}}</text>
			</view>
			<view class="homeworkItem_time">
				<text>提交时间：{{item.time}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import cloudbase from '@cloudbase/js-sdk';

	const db = cloudbase.database();
	const dbCmd = db.command;
	export default {
		data() {
			return {
				// 班别
				className: '',
				// 学生列表
				studentList: [],
				homeworkList: [],
				option: ''
			}
		},
		onLoad(e) {
			// 获取作业类型
			this.option = e.option;
			// 获取作业列表
			if (e.item) {
				let item = JSON.parse(e.item);
				this.className = item.name.split('（')[0];
				// 获取学生作业信息
				this.getStudentWork(item, e.option);
				this.homeworkList = item;
			}
		},
		methods: {
			getStudentWork: function(item, option) { 
				console.log(item);
				item.homework.forEach(work => {
					if(option == '未批改' && work.score == "") {
						this.selectStudentInfo(work);
					}else if(option == '已批改' && work.score != ""){
						this.selectStudentInfo(work);
					}
					
				})
			},
			selectStudentInfo: function(work) {
				// 查询学生姓名
				db.collection('user').where({
					openId: work.openid
				}).field({
					realName: true
				}).get().then(res => {
					console.log(res);
					let studentName = res.data[0].realName;
					// 将学生的信息放置到数组中
					this.studentList.push({
						studentName: studentName,
						time: work.time
					})
				})
			},
			jumpToHomework: function(index) {
				let homework = this.homeworkList.homework[index];
				// console.log(homework);
				let item = {
					course: this.homeworkList.course,
					endTime: this.homeworkList.endTime,
					feedback: this.homeworkList.feedback,
					homework: homework,
					name: this.homeworkList.name,
					students: this.homeworkList.students,
					subject: this.homeworkList.subject,
					time: this.homeworkList.time,
					_id: this.homeworkList._id,
					studentId: homework.openid
				}
				uni.navigateTo({
					url: "../homeworkMore/homeworkMore?homework=" + JSON.stringify(item) + '&option=' + this.option
				})
			}
		}
	}
</script>

<style>
	.page {
		width: 750rpx;
		height: 1812rpx;
		/* height: 1020rpx; */
		/* padding-bottom: 60px; */
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #F3F5F7;
	}

	.top {
		width: 100%;
		/* height: 120rpx; */
		display: flex;
		/* box-sizing: border-box; */
		justify-content: center;
		/* text-align: center; */
		font-weight: 500;
		font-size: 36rpx;
		color: #FFAA5F;
		margin: 20rpx 0;
	}

	.homeworkItem {
		width: 702rpx;
		height: 161rpx;
		margin-top: 24rpx;
		margin-right: auto;
		margin-left: auto;
		background-color: #FFFFFF;
		border-radius: 24rpx;
	}

	.homeworkItem_title {
		width: 702rpx;
		height: 42rpx;
		font-size: 32rpx;
		line-height: 42rpx;
		margin-top: 36rpx;
		margin-bottom: 15rpx;
		text-align: center;
		color: #222222;
	}

	.homeworkItem_time {
		width: 702rpx;
		height: 42rpx;
		font-size: 30rpx;
		line-height: 42rpx;
		text-align: center;
		color: #707070;
		user-select: none;
	}
</style>
